// pages/words/words.js
const wordsData = {
  "animals": [
    {"word": "cat", "chinese": "猫", "image": "/images/words/cat.png", "sound": "/sounds/word-cat.mp3"},
    {"word": "dog", "chinese": "狗", "image": "/images/words/dog.png", "sound": "/sounds/word-dog.mp3"},
    {"word": "bird", "chinese": "鸟", "image": "/images/words/bird.png", "sound": "/sounds/word-bird.mp3"},
    {"word": "fish", "chinese": "鱼", "image": "/images/words/fish.png", "sound": "/sounds/word-fish.mp3"},
    {"word": "rabbit", "chinese": "兔子", "image": "/images/words/rabbit.png", "sound": "/sounds/word-rabbit.mp3"},
    {"word": "tiger", "chinese": "老虎", "image": "/images/words/tiger.png", "sound": "/sounds/word-tiger.mp3"},
    {"word": "lion", "chinese": "狮子", "image": "/images/words/lion.png", "sound": "/sounds/word-lion.mp3"},
    {"word": "elephant", "chinese": "大象", "image": "/images/words/elephant.png", "sound": "/sounds/word-elephant.mp3"}
  ],
  "fruits": [
    {"word": "apple", "chinese": "苹果", "image": "/images/words/apple.png", "sound": "/sounds/word-apple.mp3"},
    {"word": "banana", "chinese": "香蕉", "image": "/images/words/banana.png", "sound": "/sounds/word-banana.mp3"},
    {"word": "orange", "chinese": "橙子", "image": "/images/words/orange.png", "sound": "/sounds/word-orange.mp3"},
    {"word": "grape", "chinese": "葡萄", "image": "/images/words/grape.png", "sound": "/sounds/word-grape.mp3"},
    {"word": "strawberry", "chinese": "草莓", "image": "/images/words/strawberry.png", "sound": "/sounds/word-strawberry.mp3"},
    {"word": "watermelon", "chinese": "西瓜", "image": "/images/words/watermelon.png", "sound": "/sounds/word-watermelon.mp3"},
    {"word": "pear", "chinese": "梨", "image": "/images/words/pear.png", "sound": "/sounds/word-pear.mp3"},
    {"word": "peach", "chinese": "桃子", "image": "/images/words/peach.png", "sound": "/sounds/word-peach.mp3"}
  ],
  "colors": [
    {"word": "red", "chinese": "红色", "image": "/images/words/red.png", "sound": "/sounds/word-red.mp3"},
    {"word": "blue", "chinese": "蓝色", "image": "/images/words/blue.png", "sound": "/sounds/word-blue.mp3"},
    {"word": "green", "chinese": "绿色", "image": "/images/words/green.png", "sound": "/sounds/word-green.mp3"},
    {"word": "yellow", "chinese": "黄色", "image": "/images/words/yellow.png", "sound": "/sounds/word-yellow.mp3"},
    {"word": "orange", "chinese": "橙色", "image": "/images/words/orange-color.png", "sound": "/sounds/word-orange.mp3"},
    {"word": "purple", "chinese": "紫色", "image": "/images/words/purple.png", "sound": "/sounds/word-purple.mp3"},
    {"word": "pink", "chinese": "粉色", "image": "/images/words/pink.png", "sound": "/sounds/word-pink.mp3"},
    {"word": "black", "chinese": "黑色", "image": "/images/words/black.png", "sound": "/sounds/word-black.mp3"}
  ],
  "numbers": [
    {"word": "one", "chinese": "一", "number": 1, "sound": "/sounds/word-one.mp3"},
    {"word": "two", "chinese": "二", "number": 2, "sound": "/sounds/word-two.mp3"},
    {"word": "three", "chinese": "三", "number": 3, "sound": "/sounds/word-three.mp3"},
    {"word": "four", "chinese": "四", "number": 4, "sound": "/sounds/word-four.mp3"},
    {"word": "five", "chinese": "五", "number": 5, "sound": "/sounds/word-five.mp3"},
    {"word": "six", "chinese": "六", "number": 6, "sound": "/sounds/word-six.mp3"},
    {"word": "seven", "chinese": "七", "number": 7, "sound": "/sounds/word-seven.mp3"},
    {"word": "eight", "chinese": "八", "number": 8, "sound": "/sounds/word-eight.mp3"},
    {"word": "nine", "chinese": "九", "number": 9, "sound": "/sounds/word-nine.mp3"},
    {"word": "ten", "chinese": "十", "number": 10, "sound": "/sounds/word-ten.mp3"}
  ]
};

Page({
  data: {
    categories: {
      animals: { name: '动物', icon: '🐶' },
      fruits: { name: '水果', icon: '🍎' },
      colors: { name: '颜色', icon: '🎨' },
      numbers: { name: '数字', icon: '🔢' }
    },
    categoryList: [
      { key: 'animals', name: '动物', icon: '🐶' },
      { key: 'fruits', name: '水果', icon: '🍎' },
      { key: 'colors', name: '颜色', icon: '🎨' },
      { key: 'numbers', name: '数字', icon: '🔢' }
    ],
    currentCategory: '',
    currentWords: [],
    currentIndex: 0,
    currentSound: '',
    audioContext: null
  },

  onLoad() {
    // 创建音频上下文
    this.audioContext = wx.createInnerAudioContext();
  },

  onUnload() {
    // 销毁音频上下文
    if (this.audioContext) {
      this.audioContext.destroy();
    }
  },

  // 选择分类
  selectCategory(e) {
    const category = e.currentTarget.dataset.category;
    if (!category) {
      console.error('分类参数为空');
      return;
    }
    
    let words = wordsData[category] || [];
    
    // 如果是数字分类，为每个单词生成圆圈数组
    if (category === 'numbers') {
      words = words.map(item => ({
        ...item,
        circles: Array(item.number).fill(0) // 生成对应数量的圆圈数组
      }));
    }
    
    console.log('选择分类', category, '单词数量', words.length);
    
    this.setData({
      currentCategory: category,
      currentWords: words,
      currentIndex: 0
    }, () => {
      console.log('分类数据更新完成', this.data.currentCategory, this.data.currentWords);
    });
  },

  // 滑动切换
  onSwiperChange(e) {
    this.setData({
      currentIndex: e.detail.current
    });
  },

  // 播放单词发音
  playWord(e) {
    const sound = e.currentTarget.dataset.sound;
    
    this.setData({
      currentSound: sound
    });

    // 播放音频
    if (this.audioContext) {
      this.audioContext.src = sound;
      this.audioContext.play();
    }
  },

  onAudioEnd() {
    // 音频播放结束
  }
});

